import 'package:cached_network_image/cached_network_image.dart';
import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:school_safe_teacher/constans/constants.dart';
import 'package:school_safe_teacher/constans/route_constants.dart';
import 'package:school_safe_teacher/resourse/app_colors.dart';
import 'package:school_safe_teacher/widget/my_widget.dart';
import 'package:school_safe_teacher/widget/no_message_widget.dart';

import 'action.dart';
import 'state.dart';

Widget buildView(
    NewsListState state, Dispatch dispatch, ViewService viewService) {
  return stateBar(
      child: Column(
        children: [
          Container(
            color: AppColors.toolBarColor,
            padding: EdgeInsets.only(
                left: 15,
                right: 15,
                top: MediaQuery.of(viewService.context).padding.top),
            child: Stack(
              alignment: Alignment.center,
              children: [
                Align(
                  child: Container(
                    child: Image.asset(
                      "images/member_back@2x.png",
                      height: 17,
                      width: 9,
                    ),
                    padding: EdgeInsets.only(top: 15, bottom: 15),
                  ),
                  alignment: Alignment.centerLeft,
                ),
                Align(
                    child: Row(
                  children: [
                    Expanded(
                        child: Container(
                      margin: EdgeInsets.fromLTRB(25, 5, 5, 5),
                      padding: EdgeInsets.only(
                          left: 17, right: 17, top: 5, bottom: 5),
                      alignment: Alignment.center,
                      width: MediaQuery.of(viewService.context).size.width - 50,
                      child: TextField(
                        onChanged: (value) {
                          state..key = value;
                        },
                        onSubmitted: (value){
                          state..key = value;
                          dispatch(NewsListActionCreator.onSearch());
                        },
                        autofocus: state.editFocus,
                        keyboardType: TextInputType.text,
                        decoration: InputDecoration(
                            contentPadding:
                                const EdgeInsets.symmetric(vertical: 2.0),
                            icon: Image.asset(
                              "images/home_search@2x.png",
                              width: 15,
                              height: 15,
                            ),
                            border: InputBorder.none,
                            hintText: "请输入搜索",
                            isDense: true,
                            hintStyle: TextStyle(
                                fontWeight: FontWeight.normal,
                                fontSize: 14,
                                color: AppColors.hintTextColor)),
                        textAlign: TextAlign.start,
                        textAlignVertical: TextAlignVertical.center,
                      ),
                      decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(25),
                          border: Border.all(
                              color: AppColors.editBackgroundColor, width: 1),
                          color: AppColors.editBackgroundColor),
                    )),
                    GestureDetector(
                      child: Container(
                        child: Text(
                          "搜索",
                          style: TextStyle(
                              fontWeight: FontWeight.normal,
                              fontSize: 13,
                              color: AppColors.deepTextColor),
                        ),
                      ),
                      onTap: () {
                        dispatch(NewsListActionCreator.onSearch());
                      },
                    )
                  ],
                )),
              ],
            ),
          ),
          Expanded(
              child: Container(
                  color: AppColors.MainBackColor,
                  child: EasyRefresh(
                      controller: state.easyRefreshController,
                      header: MaterialHeader(
                          completeDuration: Duration(milliseconds: 0)),
                      footer: MaterialFooter(),
                      firstRefresh: true,
                      enableControlFinishRefresh: true,
                      enableControlFinishLoad: true,
                      onRefresh: () async {
                        //下拉刷新
                        state.page = 1;
                        dispatch(NewsListActionCreator.onRefresh());
                      },
                      onLoad: () async {
                        //上拉加载
                        dispatch(NewsListActionCreator.onLoad(++state.page));
                      },
                      child: _list(state, dispatch, viewService))))
        ],
      ),
      context: viewService.context);
}

Widget _list(NewsListState state, Dispatch dispatch, ViewService viewService) {
  if (state.items.isEmpty) {
    return noMessageWidget();
  } else {
    return ListView.separated(
        padding: EdgeInsets.only(top: 15,bottom: 15),
        itemBuilder: (BuildContext context, int index) {
          String testImg = "";
          switch (state.items[index].type) {
            case Constants.NEWS_TYPE_1:
              testImg = "images/news_icon1@2x.png";
              break;
            case Constants.NEWS_TYPE_2:
              testImg = "images/news_icon2@2x.png";
              break;
            case Constants.NEWS_TYPE_3:
              testImg = "images/news_icon3@2x.png";
              break;
          }
          return GestureDetector(
            child: Container(
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(5),
                color: Colors.white,
              ),
              margin: EdgeInsets.only(top: 5, left: 15, right: 15, bottom: 5),
              padding:
                  EdgeInsets.only(top: 15, left: 10, right: 10, bottom: 15),
              child: IntrinsicHeight(
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Expanded(
                      child: Container(
                        height: double.infinity,
                        child: Column(
                          mainAxisSize: MainAxisSize.max,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              state.items[index].title,
                              style: TextStyle(
                                  color: AppColors.deepTextColor, fontSize: 15),
                              maxLines: 2,
                              textAlign: TextAlign.start,
                            ),
                            Container(
                              child: Row(
                                children: [
                                  Container(
                                    child: Image.asset(
                                      testImg,
                                      width: 27.5,
                                      height: 16.5,
                                    ),
                                  ),
                                  Container(
                                    margin: EdgeInsets.only(left: 5),
                                    child: Text(
                                      state.items[index].createTime,
                                      style: TextStyle(
                                          fontSize: 11,
                                          color: AppColors.smallTextColor),
                                    ),
                                  )
                                ],
                              ),
                            )
                          ],
                        ),
                        margin: EdgeInsets.only(right: 20),
                      ),
                      flex: 2,
                    ),
                    Expanded(
                      child: Container(
                          child: CachedNetworkImage(
                        imageUrl: state.items[index].imgUrl,
                        errorWidget: (context, url, error) =>
                            Image.asset("images/test_banner@2x.png"),
                        fit: BoxFit.fitWidth,
                      )),
                      flex: 1,
                    )
                  ],
                ),
              ),
            ),
            onTap: () {
              Navigator.of(viewService.context)
                  .pushNamed(RouteConstants.NEWS_DETAIL, arguments: {
                Constants.NEWS_DETAIL_ENTITY: state.items[index]
              });
            },
          );
        },
        separatorBuilder: (BuildContext context, int index) {
          return SizedBox(
            height: 10,
          );
        },
        itemCount: state.items.length);
  }
}
